$(function () {

    /**
     * 配置权限树
     */
    (function () {
        //配置数据
        var data = [
            {
                "id": 1,
                "pId": 0,
                "name": "咨询中心",
                "open": true,
            },
            {
                "id": 11,
                "pId": 1,
                "name": "信息管理中心",
                "target": "_blank",
                "url": "permissionField.html",
            },
            {
                "id": 12,
                "pId": 1,
                "name": "信息分发中心",
                "target": "_blank",
                "url": "permissionField.html"
            },
            {
                "id": 13,
                "pId": 1,
                "name": "咨询工作台",
                "target": "_blank",
                "url": "permissionField.html"
            },
            {
                "id": 2,
                "pId": 0,
                "name": "服务中心",
                "open": true
            },
            {
                "id": 21,
                "pId": 2,
                "name": "招生资源管理",
                "target": "_blank",
                "url": "permissionField.html"
            },
            {
                "id": 22,
                "pId": 2,
                "name": "报告管理",
                "target": "_blank",
                "url": "permissionField.html"
            },
            {
                "id": 23,
                "pId": 2,
                "name": "服务管理",
                "target": "_blank",
                "url": "permissionField.html"
            },
            {
                "id": 3,
                "pId": 0,
                "name": "财务中心",
                "isParent": true,
                "open": true
            },
            {
                "id": 31,
                "pId": 3,
                "name": "集团财务"
            },
            {
                "id": 31,
                "pId": 3,
                "name": "分校财务",
                "target": "_blank",
                "url": "permissionField.html"
            },
            {
                "id": 31,
                "pId": 3,
                "name": "中培财务",
                "target": "_blank",
                "url": "permissionField.html"
            },
            {
                "id": 4,
                "pId": 0,
                "name": "质检中心",
                "isParent": true
            },
            {
                "id": 41,
                "pId": 4,
                "name": "奖励管理"
            },
            {
                "id": 42,
                "pId": 4,
                "name": "惩罚管理"
            },
            {
                "id": 43,
                "pId": 4,
                "name": "回访管理"
            },
            {
                "id": 44,
                "pId": 4,
                "name": "查漏管理"
            },
            {
                "id": 45,
                "pId": 4,
                "name": "变更管理(学员)"
            },
            {
                "id": 5,
                "pId": 0,
                "name": "配置中心",
                "isParent": true
            },
            {
                "id": 51,
                "pId": 5,
                "name": "活动管理"
            },
            {
                "id": 52,
                "pId": 5,
                "name": "藏宝阁管理"
            },
            {
                "id": 53,
                "pId": 5,
                "name": "分校分成比例管理"
            },
            {
                "id": 54,
                "pId": 5,
                "name": "财务配置"
            },
            {
                "id": 6,
                "pId": 0,
                "name": "系统管理",
                "isParent": true
            },
            {
                "id": 61,
                "pId": 6,
                "name": "用户管理",
                "target": "_blank",
                "url": "user.html"
            },
            {
                "id": 62,
                "pId": 6,
                "name": "部门管理(职位)"
            },
            {
                "id": 63,
                "pId": 6,
                "name": "权限管理管理"
            },
            {
                "id": 64,
                "pId": 6,
                "name": "菜单管理"
            },
            {
                "id": 65,
                "pId": 6,
                "name": "字典管理",
                "isParent": true
            },
            {
                "id": 651,
                "pId": 65,
                "name": "学员属性"
            },
            {
                "id": 652,
                "pId": 65,
                "name": "区域属性"
            },
            {
                "id": 66,
                "pId": 6,
                "name": "公告管理",
                "isParent": true
            },
            {
                "id": 651,
                "pId": 66,
                "name": "制度管理"
            },
            {
                "id": 652,
                "pId": 66,
                "name": "惩罚明细"
            },
            {
                "id": 7,
                "pId": 0,
                "name": "日志管理",
                "isParent": true
            },
            {
                "id": 71,
                "pId": 7,
                "name": "操作日期"
            },
            {
                "id": 72,
                "pId": 7,
                "name": "系统日志",
            }
        ];

        var setting = {
            view: {
                selectedMulti: false,
            },
            check: {
                enable: true,
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            edit: {
                enable: true
            },
            treeNode: {
                checked: true
            }
        };

        $.fn.zTree.init($("#permissionTree"), setting, data);

        var treeObj = $.fn.zTree.getZTreeObj("permissionTree");
        treeObj.checkAllNodes(true);

        //点击角色显示右侧配置树
        $('.role-list .sidebar-menu .submenu .submenu').on('click', 'a', function () {
            $('.permission-config h4').text($(this).find('.menu-text').text().trim());
        });
    })();

    /**
     * 权限字段
     */
    (function () {
        /**
         * 全选
         * @param parentEle
         */
        function checkAll(parentEle) {
            $(parentEle).find('.checkAll').on('click', function () {
                if ($(this).prop('checked')) {
                    $(parentEle).find('.form-group input[type="checkbox"]').prop('checked', true);
                    $(parentEle).find('.inverseCheck').prop('checked', false);
                } else {
                    $(parentEle).find('.form-group input[type="checkbox"]').prop('checked', false);
                    $(parentEle).find('.inverseCheck').prop('checked', true);
                }
            });
            $(parentEle).find('.form-group input[type="checkbox"]').each(function (index, ele) {
                $(ele).on('click', function () {
                    if (!$(ele).prop('checked')) {
                        $(parentEle).find('.checkAll').prop('checked', false);
                    } else {
                        $(parentEle).find('.inverseCheck').prop('checked', false);
                    }
                });
            })
        }

        /**
         * 反选
         * @param parentEle
         */
        function inverseCheck(parentEle) {
            $(parentEle).find('.inverseCheck').on('click', function () {
                if ($(this).prop('checked')) {
                    $(parentEle).find('.form-group input[type="checkbox"]').prop('checked', false);
                    $(parentEle).find('.checkAll').prop('checked', false);
                } else {
                    $(parentEle).find('.form-group input[type="checkbox"]').prop('checked', true);
                    $(parentEle).find('.checkAll').prop('checked', true);
                }
            });
            $(parentEle).find('.form-group input[type="checkbox"]').each(function (index, ele) {
                $(ele).on('click', function () {
                    if (!$(ele).prop('checked')) {
                        $(parentEle).find('.checkAll').prop('checked', false);
                    } else {
                        $(parentEle).find('.inverseCheck').prop('checked', false);
                    }
                });
            })
        }

        checkAll('#userBasic');
        checkAll('#recordBasic');
        checkAll('#socialSecurity');
        inverseCheck('#userBasic');
        inverseCheck('#recordBasic');
        inverseCheck('#socialSecurity');
    })();
});


